<?php

class BWGModelThumbnails {
  ////////////////////////////////////////////////////////////////////////////////////////
  // Events                                                                             //
  ////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////
  // Constants                                                                          //
  ////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////
  // Variables                                                                          //
  ////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////
  // Constructor & Destructor                                                           //
  ////////////////////////////////////////////////////////////////////////////////////////
  public function __construct() {
  }
  ////////////////////////////////////////////////////////////////////////////////////////
  // Public Methods                                                                     //
  ////////////////////////////////////////////////////////////////////////////////////////
  public function get_theme_row_data($id) {
    global $wpdb;
    if ($id) {
      $row = $wpdb->get_row($wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . 'bwg_theme WHERE id="%d"', $id));
    }
    else {      
      $row = $wpdb->get_row($wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . 'bwg_theme WHERE default_theme="%d"', 1));
    }
    return $row;
  }

  public function get_gallery_row_data($id) {
    global $wpdb;
    $row = $wpdb->get_row($wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . 'bwg_gallery WHERE published=1 AND id="%d"', $id));
    return $row;
  }

  public function get_image_rows_data($id, $images_per_page, $sort_by, $bwg, $type, $sort_direction = ' ASC ') {
    global $wpdb;
    $bwg_search = ((isset($_POST['bwg_search_' . $bwg]) && esc_html($_POST['bwg_search_' . $bwg]) != '') ? esc_html($_POST['bwg_search_' . $bwg]) : '');
    $search_head=isset($_GET['bwg_search_head']) ? $_GET['bwg_search_head']: '';
    $user=get_user_by('login',$_GET['user_name']);
    $content=$_GET['content'];
    if  ($type == 'tag') {
      if ($bwg_search != '') {
        $where = 'AND image.alt LIKE "%%' . $bwg_search . '%%"'; 
      }
      else {
        $where = '';
      }
    }
    else {
      if ($bwg_search != '') {
        $where = 'AND alt LIKE "%%' . $bwg_search . '%%"';  
      }
      else {
        $where = '';
      }
    }
    //hiển thị theo trang cụ thể
    if ($search_head!='')
    {
      $join_table= ' as image left join  (SELECT id,image_id,comment FROM '.$wpdb->prefix.'bwg_IMAGE_COMMENT) as comment ON image.id=comment.image_id LEFT JOIN (SELECT id,user_login FROM '.$wpdb->prefix.'users) as user ON image.author=user.id LEFT JOIN (select post_id, count(*) as like_number from '.$wpdb->prefix.'ulike WHERE status="like" group by post_id) as likes ON image.id=likes.post_id';
      $multiple= ' AND (filename LIKE "%%'.$search_head.'%%" OR user.user_login  LIKE "%%'.$search_head.'%%" OR alt  LIKE "%%'.$search_head.'%%" OR  description  LIKE "%%'.$search_head.'%%" OR comment  LIKE "%%'.$search_head.'%%" OR date  LIKE "%%'.$search_head.'%%" OR filetype LIKE "%%'.$search_head.'%%")';
      $order_by =' like_number desc, ';
    }
    else if($content=='likes')
    {
      $join_table=' as image LEFT JOIN (select post_id, user_id, count(*) as like_number from '.$wpdb->prefix.'ulike as like_table WHERE status="like"  group by post_id) as likes ON image.id=likes.post_id';
      $multiple =' AND user_id='.$user->id;
      
      $order_by =' date desc, ';

    }
    else if($content=='images')
    {
      $join_table=' as image ';
      $multiple =' AND author='.$user->id;
      
      $order_by =' ';
    }
    else
    {
      $join_table=' as image LEFT JOIN (select post_id, count(*) as like_number from '.$wpdb->prefix.'ulike WHERE status="like" group by post_id) as likes ON image.id=likes.post_id';
      $multiple ='';
      $order_by = ' like_number desc, ';
    }

    if ($sort_by == 'size' || $sort_by == 'resolution' || $sort_by == 'filename') {
      $sort_by = ' CAST(' . $sort_by . ' AS SIGNED) ';
    }
    elseif (($sort_by != 'alt') && ($sort_by != 'date') && ($sort_by != 'filetype') && ($sort_by != 'RAND()')) {
      $sort_by = '`order`';
    }
    if (isset($_POST['page_number_' . $bwg]) && $_POST['page_number_' . $bwg]) {
      $limit = ((int) $_POST['page_number_' . $bwg] - 1) * $images_per_page;
    }
    else {
      $limit = 0;
    }
    if ($images_per_page) {
      $limit_str = 'LIMIT ' . $limit . ',' . $images_per_page;
    }
    else {
      $limit_str = '';
    }
    if($type == 'tag') {
      $query=$wpdb->prepare('SELECT distinct image.* FROM ' . $wpdb->prefix . 'bwg_image as image INNER JOIN ' . $wpdb->prefix . 'bwg_image_tag as tag ON image.id=tag.image_id WHERE image.published=1 ' . $where . $multiple.' AND tag.tag_id="%d" ORDER BY ' . $sort_by . ' ' . $sort_direction . ' ' . $limit_str, $id);
      $row = $wpdb->get_results($query);
    }
    else {
      $query=$wpdb->prepare('SELECT distinct image.* FROM ' . $wpdb->prefix . 'bwg_image'.$join_table.' WHERE published=1 ' . $where . $multiple.'  ORDER BY ' .$order_by. $sort_by . ' ' . $sort_direction . ' ' . $limit_str, $id);
      $row = $wpdb->get_results($query);
    }
    return $row;
  }

  public function page_nav($id, $images_per_page, $bwg, $type) {
    global $wpdb;
    $bwg_search = ((isset($_POST['bwg_search_' . $bwg]) && esc_html($_POST['bwg_search_' . $bwg]) != '') ? esc_html($_POST['bwg_search_' . $bwg]) : '');
    if ($type == 'tag') {
      if ($bwg_search != '') {
        $where = 'AND image.alt LIKE "%%' . $bwg_search . '%%"';
      }
      else {
        $where = '';
      }
    }
    else {
      if ($bwg_search != '') {
        $where = 'AND alt LIKE "%%' . $bwg_search . '%%"';
      }
      else {
        $where = '';
      }
    }
    if ($type == 'tag') {
      $total = $wpdb->get_var($wpdb->prepare('SELECT COUNT(*) FROM ' . $wpdb->prefix . 'bwg_image as image INNER JOIN ' . $wpdb->prefix . 'bwg_image_tag as tag ON image.id=tag.image_id WHERE image.published=1 ' . $where , $id));
    }
    else {
      $total = $wpdb->get_var($wpdb->prepare('SELECT COUNT(*) FROM ' . $wpdb->prefix . 'bwg_image WHERE published=1 ' . $where . ' AND gallery_id="%d"', $id));
    }
    $page_nav['total'] = $total;
    if (isset($_POST['page_number_' . $bwg]) && $_POST['page_number_' . $bwg]) {
      $limit = ((int) $_POST['page_number_' . $bwg] - 1) * $images_per_page;
    }
    else {
      $limit = 0;
    }
    $page_nav['limit'] = (int) ($limit / $images_per_page + 1);
    return $page_nav;
  }
  
  public function get_options_row_data() {
    global $wpdb;
    $row = $wpdb->get_row($wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . 'bwg_option WHERE id="%d"', 1));
    return $row;
  }

  ////////////////////////////////////////////////////////////////////////////////////////
  // Getters & Setters                                                                  //
  ////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////
  // Private Methods                                                                    //
  ////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////
  // Listeners                                                                          //
  ////////////////////////////////////////////////////////////////////////////////////////
}